<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="户编号" prop="fid">
        <el-input
          v-model="queryParams.fid"
          placeholder="请输入户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户主姓名" prop="name">
        <el-input
                v-model="queryParams.name"
                placeholder="请输入户主姓名"
                clearable
                @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="idCard">
        <el-input
                v-model="queryParams.idCard"
                placeholder="请输入证件号码"
                clearable
                @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="生产经营性收入" prop="opIncome">-->
<!--        <el-input-->
<!--          v-model="queryParams.opIncome"-->
<!--          placeholder="请输入生产经营性收入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="财产性收入" prop="propertyIncome">-->
<!--        <el-input-->
<!--          v-model="queryParams.propertyIncome"-->
<!--          placeholder="请输入财产性收入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="工资性收入" prop="salaryIncome">-->
<!--        <el-input-->
<!--          v-model="queryParams.salaryIncome"-->
<!--          placeholder="请输入工资性收入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="转移性收入" prop="transferIncome">-->
<!--        <el-input-->
<!--          v-model="queryParams.transferIncome"-->
<!--          placeholder="请输入转移性收入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="总收入" prop="sumIncome">-->
<!--        <el-input-->
<!--          v-model="queryParams.sumIncome"-->
<!--          placeholder="请输入总收入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="生产经营性支出" prop="expense">-->
<!--        <el-input-->
<!--          v-model="queryParams.expense"-->
<!--          placeholder="请输入生产经营性支出"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="人均纯收入" prop="avgIncome">-->
<!--        <el-input-->
<!--          v-model="queryParams.avgIncome"-->
<!--          placeholder="请输入人均纯收入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="所属年份" prop="reportYear">
        <el-input
          v-model="queryParams.reportYear"
          placeholder="请输入所属年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="登记管理员账号" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入登记管理员账号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数据状态(0-可修改 1-封停)" prop="state">-->
<!--        <el-input-->
<!--          v-model="queryParams.state"-->
<!--          placeholder="请输入数据状态(0-可修改 1-封停)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作时间" prop="opTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.opTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择操作时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['NH:income:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['NH:income:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['NH:income:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['NH:income:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="incomeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="收入数据表id" align="center" prop="id" />-->
      <el-table-column label="户编号" align="center" prop="fid" />
      <el-table-column label="户主姓名" align="center" prop="name" />
      <el-table-column label="证件号码" align="center" prop="idCard" />
<!--      <el-table-column label="生产经营性收入" align="center" prop="opIncome" />-->
<!--      <el-table-column label="财产性收入" align="center" prop="propertyIncome" />-->
<!--      <el-table-column label="工资性收入" align="center" prop="salaryIncome" />-->
<!--      <el-table-column label="转移性收入" align="center" prop="transferIncome" />-->
<!--      <el-table-column label="总收入" align="center" prop="sumIncome" />-->
<!--      <el-table-column label="生产经营性支出" align="center" prop="expense" />-->
<!--      <el-table-column label="人均纯收入" align="center" prop="avgIncome" />-->
      <el-table-column label="自然村" align="center" prop="natVillage">
        <template #default="scope">
          <dict-tag :options="base_nature_village" :value="scope.row.natVillage"/>
        </template>
      </el-table-column>
      <el-table-column label="所属年份" align="center" prop="reportYear" />
<!--      <el-table-column label="登记管理员账号" align="cen ter" prop="userId" />-->
<!--      <el-table-column label="数据状态(0-可修改 1-封停)" align="center" prop="state" />-->
<!--      <el-table-column label="操作时间" align="center" prop="opTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.opTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="修改" placement="top" v-if="scope.row.id !== 0">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                       v-hasPermi="['NH:farmer:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="scope.row.id !== 0">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['NH:farmer:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农户收入信息对话框 -->
    <el-dialog :title="title" v-model="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
        <el-form-item label="户编号" prop="fid">
          <el-input v-model="form.fid" placeholder="请输入户编号" />
        </el-form-item>
        <el-form-item label="户主姓名" prop="fid">
          <el-input v-model="form.name" placeholder="请输入户主姓名" />
        </el-form-item>
        <el-form-item label="证件号码" prop="fid">
          <el-input v-model="form.idCard" placeholder="请输入证件号码" />
        </el-form-item>
<!--        <el-form-item label="生产经营性收入" prop="opIncome">-->
<!--          <el-input v-model="form.opIncome" placeholder="请输入生产经营性收入" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="财产性收入" prop="propertyIncome">-->
<!--          <el-input v-model="form.propertyIncome" placeholder="请输入财产性收入" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="工资性收入" prop="salaryIncome">-->
<!--          <el-input v-model="form.salaryIncome" placeholder="请输入工资性收入" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="转移性收入" prop="transferIncome">-->
<!--          <el-input v-model="form.transferIncome" placeholder="请输入转移性收入" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="总收入" prop="sumIncome">-->
<!--          <el-input v-model="form.sumIncome" placeholder="请输入总收入" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="生产经营性支出" prop="expense">-->
<!--          <el-input v-model="form.expense" placeholder="请输入生产经营性支出" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="人均纯收入" prop="avgIncome">-->
<!--          <el-input v-model="form.avgIncome" placeholder="请输入人均纯收入" />-->
<!--        </el-form-item>-->
        <el-form-item label="所属年份" prop="reportYear">
          <el-input v-model="form.reportYear" placeholder="请输入所属年份" />
        </el-form-item>
<!--        <el-form-item label="登记管理员账号" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入登记管理员账号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="数据状态(0-可修改 1-封停)" prop="state">-->
<!--          <el-input v-model="form.state" placeholder="请输入数据状态(0-可修改 1-封停)" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="操作时间" prop="opTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.opTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择操作时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { listIncome, getIncome, delIncome, addIncome, updateIncome } from "@/api/NH/income";
const router = useRouter();
const {proxy} = getCurrentInstance();
const {
  base_nature_village,
} = proxy.useDict("base_nature_village");
// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 农户收入信息表格数据***
const incomeList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

const dateRange = ref([]);
const data = reactive({
      // 表单校验
      form: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fid: null,
        opIncome: null,
        propertyIncome: null,
        salaryIncome: null,
        transferIncome: null,
        sumIncome: null,
        expense: null,
        avgIncome: null,
        reportYear: null,
        userId: null,
        state: null,
        opTime: null
      },
      // 表单参数
      // 表单校验
      rules: {
        fid: [
          { required: true, message: "户编号不能为空", trigger: "blur" }
        ],
        reportYear: [
          { required: true, message: "所属年份不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "数据状态(0-可修改 1-封停)不能为空", trigger: "blur" }
        ],
        opTime: [
          { required: true, message: "操作时间不能为空", trigger: "blur" }
        ]
      }
});

const {queryParams, form, rules} = toRefs(data);



    /** 查询农户收入信息列表 */
    function getList() {
      loading.value = true;
      listIncome(queryParams.value).then(response => {
        incomeList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
    }
    // 取消按钮
   function cancel() {
     open.value = false;
     reset();
    }
    // 表单重置
   function  reset() {
      form.value = {
        id: null,
        fid: null,
        opIncome: null,
        propertyIncome: null,
        salaryIncome: null,
        transferIncome: null,
        sumIncome: null,
        expense: null,
        avgIncome: null,
        reportYear: null,
        userId: null,
        state: null,
        opTime: null
      };
      proxy.resetForm("form");
    };
    /** 搜索按钮操作 */
    function handleQuery() {
      queryParams.value.pageNum = 1;
      getList();
    };
    /** 重置按钮操作 */
    function resetQuery() {
      dateRange.value = [];
      proxy.resetForm("queryForm");
      handleQuery();
    };
    // 多选框选中数据
   function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
};
    /** 新增按钮操作 */
   function handleAdd() {
      reset();
      const id = row.id || ids.value
      getFarmer(id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "添加农户收入信息";
      });
    };
    /** 修改按钮操作 */
  function handleUpdate(row) {
      reset();
      const id = row.id || this.ids
      getIncome(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改农户收入信息";
      });
    };
    /** 提交按钮 */
   function submitForm() {
      proxy.$refs["form"].validate(valid => {
        if (valid) {
          if (form.value.id != null) {
            updateIncome(form.value).then(response => {
              proxy.$modal.msgSuccess("修改成功");
              open.value = false;
              getList();
            });
          } else {
            addIncome(form.value).then(response => {
              proxy.$modal.msgSuccess("新增成功");
              open.value = false;
              getList();
            });
          }
        }
      });
    };
    /** 删除按钮操作 */
    function handleDelete(row) {
      const ids = row.id || ids.value;
      proxy.$modal.confirm('是否确认删除农户收入信息编号为"' + ids + '"的数据项？').then(function() {
        return delIncome(ids);
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    };
    /** 导出按钮操作 */
    function handleExport() {
      proxy.download('NH/income/export', {
        ...queryParams.value
      }, `income_${new Date().getTime()}.xlsx`);
    };
getList();
</script>
